/* HUFFMAN ARCHIEVE
	WRITTEN BY EM881G
	CAN CONTAINS SOME MISTAKES
	AND TERRIBLE STYLE OF CODING
*/
#ifndef _HUF
//#define _DEBUG___
#define _HUF
#define _SIGN "HUFARC0"
//flags
#define LEN_8_B 0x80
#define NO_HUFF 0x40
#define REPL_FILE 0x80
#define EXTR_FILE 0x0F
#define DEL_FILE_FL 0xFF00
#define NEW_ARC 0xF000
#define NEW_FILE 0x0F0
#define NEW_ARC_2 0xF0000
// flags for cl_exit
#define ADD_FILES 1
#define FUNC_INI 2
#define LISTING_FILE 3
#define OPEN_ARC 4
#define TESTING 5
#define COPY_ARC 6
#define COPY 7
#define CLEAN_FILE 8
#define MAKE_FILE_HEAD 9
#define INT_WRITE 10
#define WRITE_TO_ARC 11
#define EXTR_FILE_ 12
#define DEL_FILE 13
#define HUF_TREE_INIT 14
#define MAKE_HUF_TREE 15
#define PASTE 16
#define WRITE_TO_ARC_ARC 17
#define ARC_FILE 18
//Constants
#define SIGN_LEN 7
#define NAME_LEN 1
#define SIZE_T_1 1
#define SIZE_T_2 2
#define SIZE_T_4 4
#define SIZE_T_8 8
#define SIZE_T_32 32
#define SIZE_BUF 64*1024
//Errors
#define SIGN_ERR 1
#define MEMORY_ERR 2
#define ARC_IWRITE 3
#define OPEN_FILE_ERR 4
#define OPEN_ERR 5
#define READ_ERR 6
#define WRITE_ERR 7
#define WRITE_ERR2 8
#define CS_ERR 9
#define ARC_TO_ARC 10
#define ERR_ARC_DEL 11
#define ERR_TMP_ARC_REN 12
#define ERR_ARC_REN 13
#define ERR_FILE_REN 14
#define ERR_NULL_STR 15
#define ERR_REPL_OLD_FILE 16
#define ERR_ARC_OPEN 17
#define COPY_ERR 18
#define ERR_NAME_ARC 19
#define INCORRECT_DATA 20


#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <setjmp.h>
#include <sys\stat.h>
#include <stddef.h>
#include <stdint.h>
#include <io.h>
#include "structs.h"
#include "cddc.h"



//FUNCTIONS
// calculate crc32 hasnum
uint_least32_t Crc32(const char * buf, size_t len);
// checking signature
int checkSign (FILE * arc);
// open 1/2 files - arc;
FILE ** openArc (char * arcName, char * arcnew);
// add file in arc
void addFiles (char * fileNames[]); 
// write file in arc, with complete heder for file return 0 if all is good
int writeToArc ( FILE * arc, FILE * fileToRead, fileHead * fileHd);
// write string in file
void strWrite (FILE * fileToWrite,char * buf, size_t len, unsigned int * ChkNum);
// write integer in file
void intWrite (FILE * fileToWrite, unsigned long long int num, size_t size, unsigned int * ChkNum);
// converting string in integer
unsigned long long int strToInt (char * buf,size_t size);
// listing archieve
fileHead ** listingFile (FILE * arc);
// make copy archieve with some changes
void copyArc (FILE * arc[],fileHead * list[]); 
// making header for file not for archieve
fileHead * makeFileHead (char * fileName);
// 1st function for extract file
void extractFiles (char * fileNames[]);
// space useless space in archieve
void spaceJump (FILE * arc, fileHead * list); 
// get name of file without path to it
char * getName (char * strName); 
// 2nd function for extract file (main worker)
int extrFile (FILE * fileToWrite, const fileHead * list, FILE * arc);
// function for cleaning files what was created in process
int clean_files (char * fileName,char * TMPfileName,int flag);
// delete files from archieve
void deleteFile (char * fileNames[]); 
/* copy information. Return 0 if all is good, Another if will some errors.*/
int copy (FILE* fileToRead,FILE * fileToWrite, unsigned long long int fileSize, unsigned int * ChkSum);
// clearing before exit
int cl_exit (int func,int flag, ...);
// testing file
int testing (FILE * arc);
// start useful functions
void startTestListFunc (char * arcName, int flag);

extern jmp_buf errorBuf;
extern _env envInf;

#endif